/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ModelLayer;

import DBLayer.MapName;
import java.io.InvalidObjectException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Evil
 */
public class SalesOrder
{
    @MapName(ColumnName = "SalesOrderID")
    private int salesOrderID;
    private int employeeID;
    private int customerID;
    private Actor employee;
    private Actor customer;
    private ArrayList<SalesLine> salesLines;
    private String deliveryStatus;
    private String deliveryDate;
    private String date;
    private int discountType;

    public SalesOrder(int salesOrderID, int employeeID, int customerID, Actor employee, Actor customer, ArrayList<SalesLine> salesLines, String deliveryStatus, String deliveryDate, String date, int discountType)
    {
        super();
        this.salesOrderID = salesOrderID;
        this.employeeID = employeeID;
        this.customerID = customerID;
        this.employee = employee;
        this.customer = customer;
        this.salesLines = salesLines;
        this.deliveryStatus = deliveryStatus;
        this.deliveryDate = deliveryDate;
        this.date = date;
        this.discountType = discountType;
    }

    public SalesOrder()
    {
        salesLines = new ArrayList<SalesLine>();
    }

    public void addProduct(Product product, int quantity)
    {
        for (int i = 1; i <= quantity; i++)
        {
            salesLines.add(new SalesLine(product, 1));
        }
    }

    public void removeProduct(int index)
    {
        for (int i = 0; i < salesLines.size(); i++)
        {
            if (index == i)
            {
                SalesLine salesLine = salesLines.get(i);

                int quantity = salesLine.getQuantity();
                if (quantity > 1)
                {
                    salesLine.setQuantity(quantity - 1);
                    return;
                }
                else
                {
                    salesLines.remove(salesLine);
                }
            }
        }
    }

    public Actor getCustomer()
    {
        return customer;
    }

    public void setCustomer(Actor customer)
    {
        this.customer = customer;
        try
        {
            this.customerID = ((Customer) customer.getRole(Role.Customer)).getCustomerID();
        }
        catch (InvalidObjectException ex)
        {
            Logger.getLogger(SalesOrder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getCustomerID()
    {
        return customerID;
    }

    public String getDate()
    {
        return date;
    }

    public void setDate(String date)
    {
        this.date = date;
    }

    public String getDeliveryDate()
    {
        return deliveryDate;
    }

    public void setDeliveryDate(String deliveryDate)
    {
        this.deliveryDate = deliveryDate;
    }

    public String getDeliveryStatus()
    {
        return deliveryStatus;
    }

    public void setDeliveryStatus(String deliveryStatus)
    {
        this.deliveryStatus = deliveryStatus;
    }

    public int getDiscountType()
    {
        return discountType;
    }

    public void setDiscountType(int discountType)
    {
        this.discountType = discountType;
    }

    public Actor getEmployee()
    {
        return employee;
    }

    public void setEmployee(Actor employee)
    {
        this.employee = employee;
        try
        {
            this.employeeID = ((Employee) employee.getRole(Role.Employee)).getEmployeeID();
        }
        catch (InvalidObjectException ex)
        {
            Logger.getLogger(SalesOrder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getEmployeeID()
    {
        return employeeID;
    }

    public ArrayList<SalesLine> getSalesLines()
    {
        return salesLines;
    }

    public void setSalesLines(ArrayList<SalesLine> salesLines)
    {
        this.salesLines = salesLines;
    }

    public int getSalesOrderID()
    {
        return salesOrderID;
    }

    public void setSalesOrderID(int salesOrderID)
    {
        this.salesOrderID = salesOrderID;
    }


}
